package com.itcodai.utils.stream.map;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Map的Optional用法
 * @author c-fanqj
 * @date 2022/6/23
 * @apiNote
 */
public class MapOptionalExercise {

    public static void main(String[] args) {
        //System.out.println();
        Map<String, String> someMap = new HashMap<>();
        someMap.put("jack","20");
        someMap.put("bill","35");

        Set<Map.Entry<String, String>> entries = someMap.entrySet();
        //System.out.println("entrySet：" + entries);
        //entrySet：[bill=35, jack=20]

        Set<String> keySet = someMap.keySet();
        //System.out.println("key：" + keySet);
        //key：[bill, jack]

        Collection<String> values = someMap.values();
        //System.out.println("value：" + values);
        //value：[35, 20]

        Stream<Map.Entry<String, String>> entryStream = entries.stream();
        Stream<String> valueStream = values.stream();
        Stream<String> keyStream = keySet.stream();

        Optional<String> optionalName = someMap.entrySet().stream()
                .filter(e -> "20".equals(e.getValue()))
                .map(Map.Entry::getKey)
                .findFirst();
        //System.out.println("optionalName：" + optionalName.get());
        //optionalName：jack

        optionalName = someMap.entrySet().stream()
                .filter(e -> "Non ages".equals(e.getValue()))
                .map(Map.Entry::getKey)
                .findFirst();
        //System.out.println("是否存在：" + optionalName.isPresent());
        // 是否存在：false

        someMap.put("alice", "20");
        List<String> listNames = someMap.entrySet().stream()
                .filter(e -> e.getValue().equals("20"))
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
        //System.out.println("listNames：" + listNames);
        //listNames：[alice, jack]

        List<String> listAges = someMap.entrySet().stream()
                .filter(e -> e.getKey().equals("alice"))
                .map(Map.Entry::getValue)
                .collect(Collectors.toList());
        //System.out.println("listAges：" + listAges);
        //listAges：[20]

    }

}
